clear all
set more off
set maxvar 20000


*Create dataset with selected variables from RAND longitudinal file

*Define directory with input data
cd "Q:"

*Create extract from RAND Longitudinal File (2020 V2) 
use "randhrs1992_2020v2.dta", clear

*Keep relevant variables
local constant = "hhid pn hhidpn hacohort rabyear ragender racohbyr raracem rahispan raedyrs ravetrn rabplace"
local hh = "h*absns h*icap h*atotw h*achck h*acd h*abond h*aothr h*ahous h*hhidc"
local work = "r*inlbrf r*work r*jhours r*jhour2 r*jcten r*ipena r*iearn r*slfemp r*work2 r*lbrf r*jcten r*samemp r*samejob"
local demog = "inw* r*wtresp r*wtr_nh r*wtcrnh r*mstat r*iwbeg r*agey_b r*proxy"
local ben = "r*higov r*covr r*covs r*hiothp r*govmr r*govmd"
local wb = "r*shlt r*depres r*whappy r*enlife r*fsad r*cesd r*sayret r*work62 r*work65 r*work70 r*work70a r*rplnyr r*rplnya r*jphys r*jlift r*jstoop r*jsight r*jstres r*jcocc* r*jcind*" 
keep `constant' `hh' `work' `demog' `ben' `wb' 

*Create analysis variables

*Time-invariant variables

*Sex
 gen male=.
 replace male=1 if ragender==1
 replace male=0 if ragender==2
 
 gen female=.
 replace female=1 if ragender==2
 replace female=0 if ragender==1

*Race
 gen white=.
 replace white=1 if raracem==1
 replace white=0 if raracem!=1 & raracem<=3
 
 gen black=. 
 replace black=1 if raracem==2
 replace black=0 if raracem!=2 & raracem<=3
 
 gen otherrace=. 
 replace otherrace=1 if raracem==3
 replace otherrace=0 if raracem!=3 & raracem<=3
 
*Hispanic
 gen hispanic=rahispan  

*Foreign born
 gen foreignborn=.
 replace foreignborn=0 if rabplace>=1 & rabplace<=10
 replace foreignborn=1 if rabplace==11
 
*Education   
 gen educyr=raedyrs

*Veteran Status
 gen veteran=ravetrn
 
*Time-varying variables

*Define number of waves
local maxwave=15
   
*Wave-specifc variables
forvalues i=1/`maxwave' {
  
*Household-level variables
	
	*Business assets
	 gen hh_bus_assets`i'=h`i'absns
	
	*Capital Income 
         gen hh_cap_inc`i'=h`i'icap if inw`i'==1  
	
	*Wealth
         gen wealth`i'=h`i'atotw  if inw`i'==1

	*Savings 
         egen savings`i'=rowtotal(h`i'achck h`i'acd h`i'abond h`i'aothr) if inw`i'==1

	*Home Ownership 
         gen own_home`i'=1 if h`i'ahous>0 & inw`i'==1
         replace own_home`i'=0 if h`i'ahous==0 & inw`i'==1
	 
	*Sub-household ID
	 gen hhidc`i'=h`i'hhidc

*Demographic variables

	*Marital status
         gen married`i'=.
	 replace married`i'=1 if (inlist(r`i'mstat, 1, 2)) & inw`i'==1
	 replace married`i'=0 if r`i'mstat>=3 & r`i'mstat<=8 & inw`i'==1

	 gen divorced`i'=.
	 replace divorced`i'=1 if r`i'mstat==5 & inw`i'==1
	 replace divorced`i'=0 if r`i'mstat!=5 & r`i'mstat<=8 & inw`i'==1
	 
         gen separated`i'=.
	 replace separated`i'=1 if r`i'mstat==4 & inw`i'==1
	 replace separated`i'=0 if r`i'mstat!=4 & r`i'mstat<=8 & inw`i'==1
	 
         gen widowed`i'=.
	 replace widowed`i'=1 if r`i'mstat==7 & inw`i'==1
	 replace widowed`i'=0 if r`i'mstat!=7 & r`i'mstat<=8 & inw`i'==1

         gen spouse`i'=.
	 replace spouse`i'=1 if (inlist(r`i'mstat, 1, 2, 3)) & inw`i'==1
	 replace spouse`i'=0 if r`i'mstat>=4 & r`i'mstat<=8 & inw`i'==1

        *Interview year
	 gen inwyear`i'=year(r`i'iwbeg)

        *Proxy
	 gen proxy`i'=r`i'proxy if inw`i'==1
      
	*Age, at beginning of interview month
         gen age`i'=r`i'agey_b if inw`i'==1
       
*Work variables

	*In LF
	 gen laborforce`i'=r`i'inlbrf if inw`i'==1

	*LFS
	 gen laborforcestatus`i'=r`i'lbrf if inw`i'==1
	 
	*Working    
         gen working`i'=r`i'work if inw`i'==1
	 
  	*Self-employed   
         gen selfemp`i'=r`i'slfemp if inw`i'==1
	 
	*Hours worked per week: Main + second job
         egen hrspw`i'=rowtotal(r`i'jhours r`i'jhour2) if inw`i'==1
	 
	*Hours worked per week: Main job
         egen hrspw_main`i'=rowtotal(r`i'jhours) if inw`i'==1

	*Job tenure
	 gen jcten`i'=r`i'jcten if inw`i'==1
	 
	*Labor Income
         gen income`i'=r`i'iearn if inw`i'==1	 
	 
	*Pension 
         gen pensionamt`i'=r`i'ipena  if inw`i'==1
	 gen pension`i'=pensionamt`i'
	 replace pension`i'=1 if pensionamt`i'>0 & pensionamt`i'!=.
	 
	*Works a second job    
         gen secondjob`i'=r`i'work2 if inw`i'==1
	 
*Benefits variables
	 
	*Any health ins. Government/Employer/Any other   
         gen anyhins`i'=1 if (r`i'higov==1 | r`i'covr==1 | r`i'covs==1 | r`i'hiothp==1) & inw`i'==1
         replace anyhins`i'=0 if anyhins`i'==. & inw`i'==1
   
	*Medicare
         gen mcare`i'=r`i'govmr if inw`i'==1

        *Medicaid
         gen medicaid`i'=r`i'govmd if inw`i'==1
   
	*Insurance through own employer
         gen oemphins`i'=r`i'covr if inw`i'==1																																																																																																								
     
	*Insurance through spouse's employer
         gen semphins`i'=r`i'covs if inw`i'==1
   
	*Privately purchased health insurance
         gen indhins`i'=r`i'hiothp if inw`i'==1

*Wellbeing variables

	*Says retired
	 gen saysretired`i'= r`i'sayret
	
	*Says at all retired
	 gen saysatallretired`i'=.
	 replace saysatallretired`i'=1 if r`i'sayret==1 | r`i'sayret==2
	 replace saysatallretired`i'=0 if r`i'sayret==0

	*Says fully retired
	 gen saysfullyretired`i'=.
	 replace saysfullyretired`i'=1 if r`i'sayret==1 
	 replace saysfullyretired`i'=0 if r`i'sayret==0 | r`i'sayret==2

	*Self-reported health
	 gen selfreportedhealth`i'= r`i'shlt
	 
	*Prob. working at 62
	 gen working_62_prob`i'= r`i'work62
	 
	*Prob. working at 65
	 gen working_65_prob`i'= r`i'work65
		 
	*When plans to stop working
	 gen whenplanstostopwork`i'= r`i'rplnyr
	
	*Job strains
	 gen jphys`i'= r`i'jphys
	 gen jlift`i'= r`i'jlift
	 gen jstoop`i'= r`i'jstoop
	 gen jsight`i'= r`i'jsight
	 gen jstres`i'= r`i'jstres
	 
	*Occupation groups
	 gen jcocc`i'= r`i'jcocc
	 
	*Industry
	 gen jcind`i'= r`i'jcind
	
	*Weight
	 gen wtcrnh`i'=r`i'wtcrnh if inw`i'==1
	 gen respwt`i'=r`i'wtresp if inw`i'==1
	 
   }

forvalues i=2/`maxwave' {

	*Occupation
	 gen jcocca`i'= r`i'jcocca
	
	*Employment transitions
	 gen samemp`i'=r`i'samemp if inw`i'==1
	 gen samejob`i'=r`i'samejob if inw`i'==1

	*Depressed
	 gen depressed`i'=.
	 replace depressed`i'=1 if r`i'depres==1
	 replace depressed`i'=0 if r`i'depres==0

	*Enjoys life
	 gen enjoyslife`i'=.
	 replace enjoyslife`i'=1 if r`i'enlife==1
	 replace enjoyslife`i'=0 if r`i'enlife==0
	 
	*Was happy
	 gen washappy`i'= r`i'whappy
	 
	*Felt sad
	 gen feltsad`i'= r`i'fsad
	 
	*CESD
	 gen cesd`i'= r`i'cesd

   }

forvalues i=11/`maxwave' {

	*Prob. working at 70
	 gen working_70_prob`i'= r`i'work70
	 
	*Prob. working after 70
	 gen working_70a_prob`i'= r`i'work70a
	 
}

gen whenthinkswillstopwork1= r1rplnya

forvalues i=3/`maxwave' {
	 
	*When thinks will stop working
	 gen whenthinkswillstopwork`i'= r`i'rplnya
}

forvalues i=5/`maxwave' {
	gen wtr_nh`i'=r`i'wtr_nh if inw`i'==1 
}

forvalues i=7/`maxwave' {
	 gen jcoccb`i'= r`i'jcoccb
	 gen jcindb`i'= r`i'jcindb
}

forvalues i=10/`maxwave' {
	 gen jcindc`i'= r`i'jcindc
	 gen jcoccc`i'= r`i'jcoccc	 
}
 
*Reshape data
keep hhid pn hhidpn inw* hacohort racohbyr rabyear male-jcoccc14
quietly des *11, varlist
local list=subinstr("`r(varlist)'","11","",.)
disp "`list'"
reshape long "`list'", i(hhidpn) j(year) 
   
rename year wave
rename inwyear year

gen waveyr=1992+(wave-1)*2

save hrsrandlongdata_1994_2020, replace
